package org.jeecg.modules.controller.market;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.entity.DbBaseline;
import org.jeecg.modules.entity.DbItemCount;
import org.jeecg.modules.service.IDbBaselineService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: db_baseline
 * @Author: jeecg-boot
 * @Date:   2024-06-13
 * @Version: V1.0
 */
@Api(tags="项目指标")
@RestController
@RequestMapping("/dbBaseline")
@Slf4j
public class DbBaselineController extends JeecgController<DbBaseline, IDbBaselineService> {
	@Autowired
	private IDbBaselineService dbBaselineService;
	 /**
	  * 返回指标列表
	  * @param purId
	  * @return
	  */
	 @ApiOperation(value="返回指标列表", notes="返回指标列表")
	 @GetMapping(value = "/selectByPurId")
	 public Result<DbBaseline> queryListModule(@RequestParam String purId){
		 QueryWrapper<DbBaseline> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("pur_id",purId);
		 DbBaseline dbBaseline=dbBaselineService.getOne(queryWrapper);
		 return Result.OK(dbBaseline);
	 }

	/**
	 * 分页列表查询
	 *
	 * @param dbBaseline
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "db_baseline-分页列表查询")
	@ApiOperation(value="项目指标分页列表查询", notes="项目指标分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DbBaseline>> queryPageList(DbBaseline dbBaseline,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<DbBaseline> queryWrapper = QueryGenerator.initQueryWrapper(dbBaseline, req.getParameterMap());
		Page<DbBaseline> page = new Page<DbBaseline>(pageNo, pageSize);
		IPage<DbBaseline> pageList = dbBaselineService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加或修改
	 *
	 * @param dbBaseline
	 * @return
	 */
	@AutoLog(value = "db_baseline-添加")
	@ApiOperation(value="项目指标添加", notes="项目指标添加")
	//@RequiresPermissions("org.jeecg.modules.demo:db_baseline:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DbBaseline dbBaseline) {
		String purId = dbBaseline.getPurId();
		DbBaseline baseline = dbBaselineService.getOne(new QueryWrapper<DbBaseline>().eq("pur_id", purId));
		if(baseline==null){
			dbBaselineService.save(dbBaseline);
			return Result.OK("添加成功！");
		}else{
			dbBaselineService.update(dbBaseline,new QueryWrapper<DbBaseline>().eq("pur_id", purId));
			return Result.OK("修改成功！");
		}
	}

	/**
	 *  编辑
	 *
	 * @param dbBaseline
	 * @return
	 */
	@AutoLog(value = "db_baseline-编辑")
	@ApiOperation(value="项目指标编辑", notes="项目指标编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:db_baseline:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DbBaseline dbBaseline) {
		dbBaselineService.updateById(dbBaseline);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "db_baseline-通过id删除")
	@ApiOperation(value="通过id删除项目指标", notes="通过id删除项目指标")
	//@RequiresPermissions("org.jeecg.modules.demo:db_baseline:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		dbBaselineService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "db_baseline-批量删除")
	@ApiOperation(value="批量删除项目指标", notes="批量删除项目指标")
	//@RequiresPermissions("org.jeecg.modules.demo:db_baseline:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.dbBaselineService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "db_baseline-通过id查询")
	@ApiOperation(value="通过id查询项目指标", notes="通过id查询项目指标")
	@GetMapping(value = "/queryById")
	public Result<DbBaseline> queryById(@RequestParam(name="id",required=true) String id) {
		DbBaseline dbBaseline = dbBaselineService.getById(id);
		if(dbBaseline==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(dbBaseline);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param dbBaseline
    */
    //@RequiresPermissions("org.jeecg.modules.demo:db_baseline:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DbBaseline dbBaseline) {
        return super.exportXls(request, dbBaseline, DbBaseline.class, "db_baseline");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("db_baseline:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DbBaseline.class);
    }

}
